﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using TorchView.GameContent.Data;
using TorchView.GameContent.Units;
using System.IO;

namespace TorchView.GameContent.Affixes
{
    public class Affix
    {
        public const string BLOCK_NAME = "AFFIX";

        public Affix(){
            Effects = new List<Effect>();
        }

        public static Affix LoadAffix(string name)
        {
            string filepath = PakReader.GetFiles("media/Affixes/", name + ".dat.adm", System.IO.SearchOption.AllDirectories).FirstOrDefault();
            if (filepath == null)
                return null;
            Stream stream = PakReader.OpenFile(filepath);
            if (stream == null)
                return null;
            DataBlock block = AdmSerializer.Load(stream);
            Affix affix = new Affix();
            affix.LoadFromDataBlock(block);
            return affix;
        }

        public void LoadFromDataBlock(DataBlock block)
        {
            if (block == null)
                throw new ArgumentNullException("block");
            if (block.Name != BLOCK_NAME)
                throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

            Name = block.GetPropertyValue("NAME", "");
            Prefix = block.GetPropertyValue("PREFIX", "");
            Suffix = block.GetPropertyValue("SUFFIX", "");
            Rank = block.GetPropertyValue("RANK", 0);
            MinSpawnRange = block.GetPropertyValue("MIN_SPAWN_RANGE", 0);
            MaxSpawnRange = block.GetPropertyValue("MAX_SPAWN_RANGE", 0);
            Duration = block.GetPropertyValue("DURATION", 0);
            Weight = block.GetPropertyValue("WEIGHT", 0);
            SlotsOccupy = block.GetPropertyValue("SLOTS_OCCUPY", 0);

            foreach (DataBlock subblock in block.SubBlocks)
            {
                switch (subblock.Name)
                {
                    case "UNITTYPES":
                        UnitTypes = AffixUnitTypes.LoadFromDataBlock(subblock);
                        break;

                    case "EFFECT":
                        Effect effect = new Effect();
                        effect.LoadFromDataBlock(subblock);
                        Effects.Add(effect);
                        break;
                }
            }
        }

        #region Properties
        public AffixUnitTypes UnitTypes { get; private set; }

        public List<Effect> Effects { get; private set; }

        [DisplayName("Name"), Category("Affix"), Description("")]
        public string Name { get; set; }

        [DisplayName("Prefix"), Category("Affix"), Description("")]
        public string Prefix { get; set; }

        [DisplayName("Suffix"), Category("Affix"), Description("")]
        public string Suffix { get; set; }

        [DisplayName("Rank"), Category("Affix"), Description("")]
        public int Rank { get; set; }

        [DisplayName("Min Spawn Range"), Category("Affix"), Description("")]
        public int MinSpawnRange { get; set; }

        [DisplayName("Max Spawn Range"), Category("Affix"), Description("")]
        public int MaxSpawnRange { get; set; }

        [DisplayName("Duration"), Category("Affix"), Description("")]
        public float Duration { get; set; }

        [DisplayName("Weight"), Category("Affix"), Description("")]
        public int Weight { get; set; }

        [DisplayName("Slots Occupy"), Category("Affix"), Description("")]
        public int SlotsOccupy { get; set; }
        #endregion

        public override string ToString()
        {
            string toRet = Name;
            if (Prefix.Length > 0)
                toRet += " [" + Prefix.Replace(" [ITEM]", "") + "]";
            else if (Suffix.Length > 0)
                toRet += " [" + Suffix.Replace("[ITEM] ", "") + "]";
            return toRet;
        }
    }
}
